package 笔试;

import java.util.HashSet;
import java.util.Set;

public class 不含有重复字符的最长子串的长度 {
    public int lengthOfLongestSubstring(String s) {
        if(s == null) {
            return 0;
        }
        Set<Character> set = new HashSet<Character>();
        int count = 0;
        int right = 0;

        for(int left = 0; left < s.length(); left++) {
            if(left != 0) {
                // 从while跳出，有重复元素了，开始找，删除， left 左移
                set.remove(s.charAt(left-1));
            }
            while(right < s.length() && !set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                ++right;
            }

            // 看count要不要更新
            count = Math.max(count, right-left);

        }
        return count;
    }
}
